package cn.itsource.controller;

import cn.itsource.annotation.LogAOP;
import cn.itsource.domain.User;

import cn.itsource.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @LogAOP
    @RequestMapping("/list")
    public String list(Model model) {
        List<User> userList = userService.findAll();
        model.addAttribute("users", userList);
        return "user/userList";
    }

    @RequestMapping("/delete")
    public String del(Integer id) {
        userService.delete(id);
        return "redirect:/user/list";
    }

    @RequestMapping("/jump")
    public String jump(Integer id, Model model) {
        if (id != null) {
            User userServiceOne = userService.findOne(id);
            model.addAttribute("user", userServiceOne);
            return "user/userUpdate";
        }
        return "user/userAdd";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.add(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/user/list";
    }


    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        User userServiceOne = userService.findOne(id);
        model.addAttribute("user", userServiceOne);
        return "user/userView";
    }
}
